package com.jianyun.jyzs.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.SearchBarAdapter;
import com.jianyun.jyzs.bean.ForwardConst;
import com.jianyun.jyzs.common.IntentRequestCode;
import com.jianyun.jyzs.fragment.DetailSearchFragment;
import com.jianyun.jyzs.fragment.SearchFragment;
import com.jianyun.jyzs.utils.Const;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.view.custom.SearchBarView;

import io.rong.imkit.RongIM;

/**
 * Created by yangshijun on 2018/9/14.
 */

public class SearchActivity extends FragmentActivity {
    public static final int FROM_CREATE_GROUP = 001;
    public static final int FROM_FORWARD_CONTACT = 002;
    public static final int FROM_OTHERS = 003;

    public static final int TYPE_SUMMARY_SEARCH = 0;
    public static final int TYPE_CONTACT_SEARCH = 1;
    public static final int TYPE_GROUP_SEARCH = 2;
    public static final int TYPE_CONVERSATION_SEARCH = 3;
    public static final int TYPE_MESSAGE_SEARCH = 4;
    public static final int TYPE_MESSAGE_SEARCH_NO_EDITOR = 5;
    public static final int TYPE_ADD_CONTACT_SEARCH = 6;
    public static final int TYPE_FAV_GROUP_SEARCH = 7;
    public static final int TYPE_GROUP_MEMBER_SEARCH = 8;
    public static final int TYPE_MENTION_GROUP_MEMBER_SEARCH = 9;
    public static final int TITLE_TYPE_MORE_CONVERSATIONS = 10;
    public static final int TITLE_TYPE_CONVERSATION_DETAILS = 11;
    public static final int TYPE_SELECTED_CONTACT_MANAGEMENT = 12; //处理已选择的联系人，搜索删除；
    public static final int TYPE_ADD_FRIENDS_SEARCH = 13;
    public static final int TYPE_FORWARD_MORE = 14; //转发部分更多联系人选择
    public static final int TYPE_CREATE_GROUP = 15; //创建群组选择联系人


    public static final String TYPE = "type";
    public static final String SEARCH_TEXT = "search_text";
    public static final String USER_SHOW_NAME = "user_show_name";
    public static final String USER_PORTRAIT = "user_portrait";
    public static final String CONVERSATION_TYPE = "conversation_type";
    public static final String CONVERSATION_ID = "conversation_id";
    public static final String DEPART_ID = "departId";
    public static final String GROUP_ID = "groupId";
    public static final String FROM = "from";

    public static final String FRAGMENT_TAG = "searchDetailFragment";

    public static final String TITLE_TYPE = "titleType";//区分查看更多聊天和聊天详情，来分别展示不同的title；

    public static final String SELECTED_CONTACT_LIST = "selectedContact";


    private TextView searchTitle;
    private SearchBarView searchBar;
    private SearchFragment searchFragment;
    private boolean createNewChat = true;

    public int getType() {
        return type;
    }

    private int type;
    private boolean contactSelectable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rce_activity_search);
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(Activity.RESULT_OK);
                finish();
            }
        });
        searchTitle = (TextView) findViewById(R.id.search_title);
        searchBar = (SearchBarView) findViewById(R.id.search_bar);

    }

    @Override
    public void onBackPressed() {
        setResult(Activity.RESULT_OK);
        super.onBackPressed();
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onPause() {
        super.onPause();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchBar.getWindowToken(), 0);
    }






    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.getCurrentFocus() != null && event.getAction() == MotionEvent.ACTION_UP) {
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == IntentRequestCode.SEARCH_DETAIL) {
                if (searchFragment instanceof DetailSearchFragment) {
                    searchFragment.onActivityResult(requestCode, resultCode, data);
                }
            } else {

                finish();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
